<template>
  <div>
    <vxe-toolbar id="shipmentManageListId" :setting="{ storage: true }" export :refresh="{ query: findList }">
      <template v-slot:buttons>
        <el-button @click="funcFilter">查询</el-button>
        <el-button   v-if="branch.level == 0"  @click="funcAdd">新增</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      row-id="id"
      height="700"
      :show-footer="true"
      :loading="loading"
      align="center"
      :data="tableData"
      remote-sort
      @sort-change="sortList"
    >
      <vxe-table-column>
        <vxe-table-column
          field="authStatus"
          show-header-overflow
          show-overflow
          title="审核状态"
          :formatter="formatterAuthStatus"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-select v-model="listQuery.authStatus" placeholder="请选择" clearable>
            <el-option
              v-for="item in authStatusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="userName"
          show-header-overflow
          show-overflow
          title="姓名"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.userName" placeholder="姓名" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="phone"
          show-header-overflow
          show-overflow
          title="手机号"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.phone" placeholder="手机号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="serialNumber"
          show-header-overflow
          show-overflow
          title="入驻商编码"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-input v-model="listQuery.serialNumber" placeholder="入驻商编码" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="taxPoint"
          show-header-overflow
          show-overflow
          title="税点(%)"
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.taxPointStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.taxPointEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="isMonthlyClient"
          :formatter="formatterCommonStatus"
          show-header-overflow
          show-overflow
          title="是否月结"
          min-width="130"
          sortable
        />
        <template v-slot:header>
          <el-select v-model="listQuery.isMonthlyClient" clearable placeholder="请选择">
            <el-option
              v-for="item in commonStatusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="isIndependentCheckout"
          show-header-overflow
          show-overflow
          title="是否独立核算"
          min-width="130"
          :formatter="formatterCommonStatus"
          sortable
        />
        <template v-slot:header>
          <el-select v-model="listQuery.isIndependentCheckout" clearable placeholder="请选择">
            <el-option
              v-for="item in commonStatusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="amount"
          show-header-overflow
          show-overflow
          title="账户金额(元)"
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.amountStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.amountEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="ensureFee"
          show-header-overflow
          show-overflow
          title="保证金基准(元)"
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.ensureFeeStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.ensureFeeEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="ensureFeeAmount"
          show-header-overflow
          show-overflow
          title="保证金余额(元)"
          min-width="200"
          sortable
        />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.ensureFeeAmountStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.ensureFeeAmountEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtCreated"
          show-header-overflow
          show-overflow
          title="创建时间"
          min-width="420"
          sortable
          :resizable="false"
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'gmtCreated')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="authTime"
          show-header-overflow
          show-overflow
          title="审核时间"
          min-width="420"
          sortable
          :resizable="false"
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtAuthRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'authTime')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column show-header-overflow show-overflow title="操作" min-width="100" fixed="right">
        <template slot-scope="scope">
          <el-tooltip   v-if="branch.level == 0"   class="item" effect="dark" content="编辑" placement="top">
            <vxe-button type="text" @click="funcEdit(scope.row)">
              <i class="el-icon-edit" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip   v-if="branch.level == 0 && scope.row.authStatus !== 3"  class="item" effect="dark" content="删除" placement="top">
            <vxe-button type="text" @click="funcDelete(scope.row.code)">
              <i class="el-icon-delete" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :page-sizes="Pagingconf"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import XEUtils from "xe-utils";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, XEUtils, { mounts: ["cookie"] });
import { getShipperList, delShipperInfo } from "@/api/shipment";
export default {
  name: "ShipmentList",
  data() {
    return {
      branch: this.$store.getters.branch,
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      gmtCreatedRange: "",
      gmtAuthRange: "",
      loading: false,
      tableData: [],
      listQuery: {
        staffType: "0", // 类型（入驻商）
        pageNum: 1,
        pageSize: 20,
        sortField: "",
        sortType: "",
        serialNumber: '',
        ensureFeeStart: "",
        ensureFeeEnd: "",
        ensureFeeAmountStart: "",
        ensureFeeAmountEnd: "",
        total: 0
      },
      Pagingconf: [],
      sumObject: {
        freezingAmount: 0,
        amount: 0
      },
      // 审核状态
      authStatusOptions: [
        { label: "未审核", value: 0, color: "gray" },
        { label: "审核中", value: 1, color: "#0000ff" },
        { label: "审核未通过", value: 2, color: "#ff0000" },
        { label: "审核通过", value: 3, color: "#00bd93" }
      ],
      // 通用是否状态
      commonStatusOptions: [
        { label: "否", value: 0 },
        { label: "是", value: 1 }
      ]
    };
  },
  created() {
    this.findList();
  },
  methods: {
    findList() {
      this.loading = true;
      return new Promise(resolve => {
        getShipperList(this.listQuery).then(response => {
          this.tableData = response.data.records;
          this.listQuery.total = response.data.total;
          this.Pagingconf = [20,30,50,100,response.data.total];
          this.sumObject = response.data.sumObject;
          this.loading = false;
          resolve()
        });
      })
    },
    // 时间
    changeDate(value, prefix) {
      if (value) {
        this.listQuery[`${prefix}Start`] = value[0] + " 00:00:00";
        this.listQuery[`${prefix}End`] = value[1] + " 23:59:59";
      } else {
        this.listQuery[`${prefix}Start`] = "";
        this.listQuery[`${prefix}End`] = "";
      }
    },
    // 排序
    sortList({ column, property, order }) {
      this.listQuery.sortField = property;
      this.listQuery.sortType = order;
      this.findList();
    },
    // 查询
    funcFilter() {
      this.listQuery.pageNum = 1
      this.findList()
    },
    // 添加
    funcAdd() {
      this.$router.push({
        path: "/basicInfo/shipment/addShipmentForm"
      });
    },
    // 编辑
    funcEdit(row) {
      this.$router.push({
        path: "/basicInfo/shipment/editShipmentForm",
        query: { code: row.code, partyCode: row.partyCode }
      });
    },
    // 删除方法
    funcDelete(codes) {
      this.$confirm("确认删除吗？")
        .then(_ => {
          delShipperInfo({ codes }).then(response => {
            this.$message({ message: response.msg, type: "success" });
            this.findList();
          });
        })
        .catch(_ => {});
    },
    formatterAuthStatus({ cellValue }) {
      const item = this.authStatusOptions.find(
        item => item.value === cellValue
      );
      return item ? item.label : "";
    },
    // 翻译通用是否状态
    formatterCommonStatus({ cellValue }) {
      const item = this.commonStatusOptions.find(
        item => item.value === cellValue
      );
      return item ? item.label : "";
    },
    // 分页handle
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.pageNum = currentPage;
      this.listQuery.pageSize = pageSize;
      this.findList();
    },
    // 合计
    footerMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return "合计";
          }
          if (column.property === "amount") {
            return "待定";
          }
          if (column.property === "ensureFee") {
            return "待定";
          }
          if (column.property === "ensureFeeAmount") {
            return "待定";
          }
          return "-";
        })
      ];
    }
  }
};
</script>

<style>


</style>
